class LineDrawTool {
    constructor(viewer) {
        this._viewer = viewer
        this._drawHandler = null
        this._tempDrawDataSource = null
        this._drawedDataSource = null
        this._tempLinePoints = []
        this._hoverPoint = null
        this._drawingFlag = false
        this.init()
    }
    async init() {
        this._drawHandler = new Cesium.ScreenSpaceEventHandler(this._viewer.canvas)
        this._tempDrawDataSource = await this._viewer.dataSources.add(new Cesium.CustomDataSource('temp-draw-data-source'))
        this._drawedDataSource = await this._viewer.dataSources.add(new Cesium.CustomDataSource('drawed-data-source'))
    }
    active() {
        this._drawingFlag = true
        this.setViewerCursorStyle('crosshair')
        //
        this._viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
            Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
        );
        //
        this._drawHandler.setInputAction((event) => {
            this._drawingFlag = true
            const ray = this._viewer.camera.getPickRay(event.position);
            const earthPosition = this._viewer.scene.globe.pick(ray, this._viewer.scene);
            if (Cesium.defined(earthPosition)) {
                this.createPoint(earthPosition, this._tempDrawDataSource);
                this._tempLinePoints.push(earthPosition)
            }
            if (this._tempLinePoints.length == 1) {
                this.createPolyline(new Cesium.CallbackProperty(() => {
                    return this._hoverPoint ? [...this._tempLinePoints, this._hoverPoint] : this._tempLinePoints
                }, false), this._tempDrawDataSource)
            }
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        //
        this._drawHandler.setInputAction((event) => {
            if (this._drawingFlag && this._tempLinePoints.length > 0) {
                const ray = this._viewer.camera.getPickRay(event.endPosition);
                const newPosition = this._viewer.scene.globe.pick(ray, this._viewer.scene);
                if (Cesium.defined(newPosition)) {
                    this._hoverPoint = newPosition
                }
            }
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
        //
        this._drawHandler.setInputAction(() => {
            if (this._drawingFlag) {
                this.deActive()
            }
        }, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }
    deActive() {
        this._drawingFlag = false
        this._viewer.cesiumWidget.screenSpaceEventHandler.setInputAction(() => { },
            Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
        );
        // this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
        // this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE)
        // this._drawHandler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK)
        // this._hoverPoint = null
        this._tempDrawDataSource.entities.removeAll()
        this.createPolyline(this._tempLinePoints, this._drawedDataSource)
        this._tempLinePoints = []
        this._hoverPoint = null
    }
    setViewerCursorStyle(style) {
        Array.from(document.getElementsByClassName('cesium-widget')).forEach(ele => {
            console.log(ele.style.cursor = style)
        })
    }
    createPoint(worldPosition, dataSource) {
        return dataSource.entities.add({
            position: worldPosition,
            point: {
                color: Cesium.Color.RED,
                pixelSize: 5,
                heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
            },
        });
    }
    createPolyline(worldPositions, dataSource) {
        return dataSource.entities.add({
            polyline: {
                positions: worldPositions,
                clampToGround: true,
                width: 3,
            },
        })
    }

}

export default LineDrawTool